package com.cardone.common.converter;

import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dozer.DozerEventListener;
import org.dozer.event.DozerEvent;
import org.dozer.fieldmap.FieldMap;

/**
 * Created by c100000002 on 15-1-29.
 */
public class ObjectEventListener implements DozerEventListener {
    @Setter
    private String defaultFieldMapCustomConverterId = "objectConverter";

    @Override
    public void mappingStarted(DozerEvent event) {
        if (CollectionUtils.isNotEmpty(event.getClassMap().getFieldMaps())) {
            for (FieldMap fieldMap : event.getClassMap().getFieldMaps()) {
                if (StringUtils.isNotBlank(fieldMap.getCustomConverter())) {
                    continue;
                }

                if (StringUtils.isNotBlank(fieldMap.getCustomConverterId())) {
                    continue;
                }

                if (StringUtils.isNotBlank(fieldMap.getMapId())) {
                    continue;
                }

                if (StringUtils.isNotBlank(fieldMap.getDateFormat())) {
                    if (StringUtils.isBlank(fieldMap.getCustomConverterParam())) {
                        fieldMap.setCustomConverterParam("dateFormat:" + fieldMap.getDateFormat());
                    }
                }

                fieldMap.setCustomConverterId(defaultFieldMapCustomConverterId);
            }
        }
    }

    @Override
    public void preWritingDestinationValue(DozerEvent event) {
    }

    @Override
    public void postWritingDestinationValue(DozerEvent event) {
    }

    @Override
    public void mappingFinished(DozerEvent event) {

    }
}
